package com.flute.framework;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StandardDate {

	private Date date;

	public StandardDate() {
		this.date = new Date();

	}

	public StandardDate(long time) {
		this.date = new Date(time);
	}

	public StandardDate(Date date) {
		this.date = date;
	}

	public StandardDate(String date) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			this.date = format.parse(date);
		} catch (ParseException e) {
			format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
			try {
				this.date = format.parse(date);
			} catch (ParseException e1) {
				throw e1;
			}
		}
	}

	public StandardDate(String date, String patern) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(patern);
		try {
			this.date = format.parse(date);
		} catch (ParseException e) {
			throw e;
		}
	}

	public String toNormalDateString() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	public String toDateByPattern(final String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	public String toSpecialDateString() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
		return format.format(date);
	}

	public String toDateStringByPattern(String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	public static StandardDate toDateByPattern(String dateString, String pattern)
			throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return new StandardDate(format.parse(dateString));
	}

	public boolean isBefore(StandardDate standardDate) {
		return this.date.before(standardDate.date);
	}

	public boolean isAfter(StandardDate standardDate) {
		return this.date.after(standardDate.date);
	}

	public boolean between(StandardDate date1, StandardDate date2) {
		return date1.date.equals(date) || date2.date.equals(date)
				|| (date1.date.before(date) && date2.date.after(date));
	}

	public boolean between(String date1, String date2) {
		try {
			Date d1 = new StandardDate(date1).date;
			Date d2 = new StandardDate(date2).date;
			return d2.equals(date) || d1.equals(date)
					|| (d1.before(date) && d2.after(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return false;
	}
}
